package com.javabase.hutooltest;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import lombok.extern.slf4j.Slf4j;

import java.security.KeyPair;

/**
 * Hutool工具测试
 * @author ronghl
 * @date 2021/01/26
 */
@Slf4j
public class HuTest {

    public static void main(String[] args) {
        testAes();

        generateKeys();

        testRsa();
    }

    /**
     * 测试aes加密/解密
     */
    public static void testAes() {
        String source = "123456";
        String salt = "timo";
        String target = salt + source;
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
        AES aes = SecureUtil.aes(key);
        String encryptHex = aes.encryptHex(target);
        String decryptHex = aes.decryptStr(encryptHex);
        log.info("source={}", target);
        log.info("encryptHex={}, decryptHex={}", encryptHex, decryptHex);
        System.out.println("");
    }

    /**
     * 测试生成密钥对
     */
    public static void generateKeys() {
        KeyPair keyPair = SecureUtil.generateKeyPair("rsa");
        String privateKey = Base64.encode(keyPair.getPrivate().getEncoded());
        String publicKey = Base64.encode(keyPair.getPublic().getEncoded());
        log.info("privateKey:");
        log.info("{}:", privateKey);
        log.info("publicKey:");
        log.info("{}:", publicKey);
        System.out.println("");
    }

    /**
     * 测试rsa加解密
     */
    public static void testRsa() {
        String pr = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALQKj/O/5fCUwT1oY94IZHZZN+/Rwng2kBrGDTQx+R77/Fp0svsoWTMaXAITS4R6sLcfsA5/UxNwOhdOItK5syTE5BYZS4qu99evGYpZ+OCk6oOunb2IrJQwoR2SRjIa7Ek7jvD5I0pkzeW5VKomSuNS0uK+T0eZCA3NTyjsUGPvAgMBAAECgYBXBxAEdAszGsS56T708buWFZRbYVqOf1lzB68UPt+74XRfSS/HBf7wG9rDyg3j8OaA7llM+LDwoXwDDer5T3hPCcPOfn+t7yazEA4c5gjbCb7GYjkhFlA1EkSa7m3/hRfHhCgEsPWj17jYuWgYxtMFSFrCsj5lXIGjT7XPdO+u+QJBAPEvbVjnn/cr+leEu+2MhO/sSwepahTne1MXLvd6u+NP6QG5WQANthbSEVMqzm1tQ+npe7A0RkJdZebSK/ARDqMCQQC/GamMkKZvMOJe2lHs7BhSfpaDg8leFgafYcJs2sBmkUaRreSCSCsaOAvRJF6pBTgTAGiv/A7Vr03PUZ+3O+ZFAkEA20yqenoxo4djW4yfbAGnJpiOKGtMv7EzG3wrAm+gw7F3RlAHstrExL9BbYOwqyL864WXJ8KAV/ugml5kASchTQJAcie1Rbe/BFryTvEh0WKzoMvnw4Y9CwwjRqBSUU5Xcq3DtxBmmYDc5pumS3fraBIrDgbHgqESh4lh9L9wpjeM9QJBAJ8iyOqpWPqiBF+Qw+KHr7SkIRCiS7mzGnfRDZ/jwKBFV++6N7bqmVg7/gOmputJ03hmMpMyeVjlI5nI/Tgbhtc=";
        String pb = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC0Co/zv+XwlME9aGPeCGR2WTfv0cJ4NpAaxg00Mfke+/xadLL7KFkzGlwCE0uEerC3H7AOf1MTcDoXTiLSubMkxOQWGUuKrvfXrxmKWfjgpOqDrp29iKyUMKEdkkYyGuxJO47w+SNKZM3luVSqJkrjUtLivk9HmQgNzU8o7FBj7wIDAQAB";
        RSA myRsa = new RSA(pr, pb);

        String source = "123456";
        String enPwd = myRsa.encryptHex(source, KeyType.PublicKey);
        String dePwd = myRsa.decryptStr(enPwd, KeyType.PrivateKey);
        log.info("source={}", source);
        log.info("encode={}", enPwd);
        log.info("decode={}", dePwd);
        System.out.println("");
    }

}
